package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.user.client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.Map;

/**
 * 订单管理
 */
@Controller
public class OrderController {

    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private OrderFeignClient orderFeignClient;

    //去结算  看起来没有传参，实际上有一个用户id的入参
    @GetMapping("/trade.html")
    public String trade(Model model){

        //1.根据用户id查询收货地址集合
        List<UserAddress> userAddressList = userFeignClient.userAddressList();
        model.addAttribute("userAddressList",userAddressList);
        //2.交易号，订单号 只能提交一次，保证幂等性，防止生成两个订单
        //3.商品清单
        Map<String, Object> trade = orderFeignClient.trade();
        if (null == trade){
            return "redirect:http://cart.gmall.com/cart.html";
        }
        model.addAllAttributes(trade);

        return "order/trade";
    }
}
